package com.etime.shuhuiyuan.controllers.demo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.Demo;
import com.etime.shuhuiyuan.services.demo.interfaces.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * demo示例的controller控制层
 */
@Api(tags = "demo案例接口")
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;


    @ApiOperation("添加demo案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="demoid",value = "自增长主键，不传递"),
            @ApiImplicitParam(name="demoname",value="传递字符串"),
            @ApiImplicitParam(name="demostate",value = "状态，0-不可用；1-正常")
    })
    @GetMapping("/insert")
    public BaseMessage insertDemo(Demo demo){
        BaseMessage msg = new BaseMessage();
        int rtn = demoService.insertDemo(demo);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("保存demo成功");
        }else{
            msg.setMsg("保存demo失败");
        }
        return msg;
    }

    @ApiOperation("修改demo案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="demoid",value = "demoid，主键，必须传递"),
            @ApiImplicitParam(name="demoname",value="传递字符串"),
            @ApiImplicitParam(name="demostate",value = "状态，0-不可用；1-正常")
    })
    @GetMapping("/update")
    public BaseMessage updateDemo(Demo demo){
        BaseMessage msg = new BaseMessage();
        int rtn = demoService.updateDemo(demo);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("保存demo成功");
        }else{
            msg.setMsg("保存demo失败");
        }
        return msg;
    }
    @ApiOperation("查询demo列表案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="demoname",value="传递字符串"),
            @ApiImplicitParam(name="demostate",value = "状态，0-不可用；1-正常")
    })
    @GetMapping("/list")
    public ListMessage<Demo> selectDemoList(Demo demo){
            ListMessage<Demo> listMessage = new ListMessage<>();
            List<Demo> list = demoService.selectDemoList(demo);
            if(list!=null) {
                listMessage.setDatalist(list);
                listMessage.setCode(1);
            }else{
                listMessage.setCode(0);
                listMessage.setMsg("没有查询到数据");
            }
            return listMessage;
    }

    @ApiOperation("分页查询demo案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="demoname",value="传递字符串"),
            @ApiImplicitParam(name="demostate",value = "状态，0-不可用；1-正常"),
            @ApiImplicitParam(name = "pagenum",value = "第几页"),
            @ApiImplicitParam(name = "pagesize",value = "每页多少行")}
    )
    @GetMapping("/page")
    public PageMessage<Demo> selectRoleByPage(Demo demo, Long pagenum, Long pagesize){
        Page<Demo> pagedata = demoService.selectDemoByPage(demo,pagenum,pagesize);
        PageMessage<Demo> msg = new PageMessage<>();
        if(pagedata!=null) {
            msg.setCode(1);
            msg.setPagedata(pagedata);
        }else{
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据id查询一条demo信息的案例")
    @ApiImplicitParam("demoid")
    @GetMapping("/get/{demoid}")
    public PojoMessage<Demo> selectDemoById(@PathVariable("demoid") int demoid){
        PojoMessage<Demo> msg = new PojoMessage<>();
        Demo demo = demoService.selectDemoByid(demoid);
        if(demo!=null){
            msg.setCode(1);
            msg.setData(demo);
        }else{
            msg.setCode(0);
            msg.setMsg("没有找到指定数据");
        }
        return msg;
    }
}
